<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      inline
      label-width="40px"
      @submit.native.prevent
    >
      <el-form-item label="系统" prop="systemId">
        <el-select
          v-model="queryParams.systemId"
          placeholder="请选择系统"
          filterable
          @change="getDevList"
        >
          <el-option
            v-for="item in systemList"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="设备" prop="devId">
        <el-select v-model="queryParams.devId" placeholder="请先选择系统" filterable>
          <el-option
            v-for="item in devList"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQueryList"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-tabs v-model="queryParams.userType" @tab-click="handleQuery">
      <el-tab-pane
        v-for="i in typeList"
        :key="i.value"
        :label="i.label"
        :name="i.value"
      />
    </el-tabs>
    <el-table v-loading="loading" :data="dataList">
      <el-table-column label="序号" align="center" type="index" />
      <el-table-column label="标准名称" align="center" prop="name" />
      <el-table-column label="AI识别名称" align="center">
        <template slot-scope="scope">
          {{ scope.row.aiName.length > 0 ? scope.row.aiName.join(",") : "--" }}
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <el-button size="mini" @click="handleEdit(scope.row)" type="text">
            编辑
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getListDiff"
    />
    <Form v-if="formShow" ref="Form" @refresh="refresh" />
  </div>
</template>

<script>
import { getDeviceList, getSystemList } from "@/api/system/ai";
import Form from "./form.vue";

export default {
  name: "Template",
  components: {
    Form,
  },
  data() {
    return {
      loading: false, // 遮罩层
      total: 0, // 总条数
      dataList: [], // 数据列表
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        systemId: null,
        devId: null,
        userType: "00",
      },
      systemList: [], // 系统列表
      devList: [], // 设备列表
      typeList: [
        { value: "00", label: "系统" },
        { value: "01", label: "设备" },
      ],
      formShow: false, // 编辑弹窗
      state: 0,
    };
  },
  created() {
    this.getList();
    this.getListAll();
  },
  methods: {
    /** 查询模板管理列表 */
    getList() {
      this.loading = true;
      getSystemList(this.queryParams)
        .then((res) => {
          this.dataList = res.rows;
          this.total = res.total;
        })
        .finally(() => {
          this.loading = false;
        });
    },
    getListDev() {
      this.loading = true;
      getDeviceList(this.queryParams)
        .then((res) => {
          this.dataList = res.rows;
          this.total = res.total;
        })
        .finally(() => {
          this.loading = false;
        });
    },
    /** 查询系统下拉列表(所有) */
    getListAll() {
      getSystemList({
        pageNum: 1,
        pageSize: 9999999,
      }).then((res) => {
        this.systemList = res.rows;
      });
    },
    /** 查询设备下拉列表(所有) */
    getDevList() {
      getDeviceList(this.queryParams).then((res) => {
        this.devList = res.rows;
      });
    },
    handleQueryList() {
      this.queryParams.pageNum = 1;
      if (!this.queryParams.devId) {
        this.queryParams.userType = "00";
        let row = this.systemList.find((i) => i.id == this.queryParams.systemId);
        if (row) {
          let name = row.name;
          getSystemList({ name }).then((res) => {
            this.dataList = res.rows;
            this.total = res.total;
          });
        }
      } else {
        this.queryParams.userType = "01";
        this.getListDev();
      }
    },
    // 分页
    getListDiff() {
      if (this.queryParams.userType == "00") {
        this.getList();
      } else {
        this.getListDev();
      }
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      if (this.queryParams.userType == "00") {
        this.state = 0;
        this.getList();
      } else {
        this.state = 1;
        this.getListDev();
      }
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.devList = [];
      this.handleQuery();
    },
    refresh() {
      this.formShow = false;
      if (this.state == 0) {
        this.getList();
      } else {
        this.getListDev();
      }
    },
    // 添加
    handleEdit(row) {
      this.formShow = true;
      this.$nextTick(() => {
        this.$refs.Form.init(row);
      });
    },
  },
};
</script>
